import './index.html';
import 'babel-polyfill';

import * as React from 'react';
import * as ReactDOM from 'react-dom';
// import { createSelector } from 'reselect';
import { addLocaleData, IntlProvider } from 'react-intl';
import { LocaleProvider, message } from 'antd';
import { createStore, combineReducers, applyMiddleware } from 'redux';
//import thunk from 'redux-thunk';
import { createCookieMiddleware } from 'redux-cookie';
import { persistStore, autoRehydrate } from 'redux-persist';
import { createFilter } from 'redux-persist-transform-filter';
import dva, { DvaInstance } from 'dva';
import { browserHistory } from 'dva/router'
import * as createLoading from 'dva-loading';
import * as Cookies from 'js-cookie';
// css
require('antd/dist/antd.less');
// 加载公共样式
require('./index.less');

import router from './router';
// 引入dva-loading
// 加载model
import { app as appModel, login, user } from './models';
// logger
// import './utils/logger';
import { Logger, settings, CookieStorage } from './utils';
// i18n
import { getLocale } from './utils/i18n';

// loger
const logger = Logger.get('index');
// redux-persist-transform-filter filter
const appSubsetFilter = createFilter(
  'app',
  ['siderFold', 'darkTheme', 'isNavbar', 'navOpenKeys'],
);

// i18n
const appLocale = getLocale('zh');
//logger.log(appLocale);
addLocaleData(appLocale.data);
const intlProvider = new IntlProvider({
  locale: appLocale.locale,
  messages: appLocale.messages
});
const { intl } = intlProvider.getChildContext();

// 新建dva对象
const app: DvaInstance = dva({
  history: browserHistory,
  initialState: window.__INITIAL_STATE__,
  onError (error) {
    // error的时候终止loadingMessage
    settings.hideLoadingMsg();
    message.error(error.message);
  },
  extraReducers: {
    intl: (state, action) => {
      // 把intk注入到dva的reducer里面
      return intl;
    }
  },
  extraEnhancers: [
    applyMiddleware(createCookieMiddleware(Cookies)),
    autoRehydrate(),
  ],
  // thunk
  // extraEnhancers: [applyMiddleware(thunk)],

});
// dva-loading
app.use(createLoading({
  effects: true
}));
// 设置model
app.model(appModel);
app.model(login);
app.model(user);

// 设置router
app.router(router);

// i18n
//const appLocale = getLocale('zh');
//logger.log(appLocale);

//app.start('#root');
app.start();
// redux-persist config
const persistConfig = {
  whitelist: ['app'],
  transforms: [
    appSubsetFilter,
  ],
  storage: new CookieStorage((app as any)._store.dispatch)
};
persistStore((app as any)._store, persistConfig, () => {
  const App = (app as any)._getProvider((app as any)._router);
  ReactDOM.render(
      <LocaleProvider locale={appLocale.antd}>
        <IntlProvider locale={appLocale.locale} messages={appLocale.messages}>
          <App />
        </IntlProvider>
      </LocaleProvider>,
      document.getElementById('root')
  );
});

// syncTranslationWithStore(store);
// store.dispatch(loadTranslations(i18n));
// loadTranslations(i18n)(store.dispatch);
// store.dispatch(setLocale('zh'));
// setLocale('zh')(store.dispatch);
